package com.zijie.hosptal.util;

import java.util.HashMap;
import java.util.Map;

/**
 * ThreadLocal 工具类 - 改进版
 */
@SuppressWarnings("all")
public class ThreadLocalUtil {
    // 提供ThreadLocal对象，存储Map
    private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL =
        ThreadLocal.withInitial(HashMap::new);

    // 根据键获取值
    public static <T> T get(String key) {
        Map<String, Object> map = THREAD_LOCAL.get();
        return (T) map.get(key);
    }

    // 存储键值对
    public static void set(String key, Object value) {
        Map<String, Object> map = THREAD_LOCAL.get();
        map.put(key, value);
    }

    // 获取所有数据
    public static Map<String, Object> getAll() {
        return THREAD_LOCAL.get();
    }

    // 清除指定键的值
    public static void remove(String key) {
        Map<String, Object> map = THREAD_LOCAL.get();
        map.remove(key);
    }

    // 清除ThreadLocal 防止内存泄漏
    public static void remove() {
        THREAD_LOCAL.remove();
    }
}
